﻿@model Orchard.Autoroute.ViewModels.AutoroutePartEditViewModel
@using Orchard.Autoroute
@using Orchard.Utility.Extensions;
@using Orchard.Environment.Configuration
@using Orchard.ContentManagement;

@if(Model.Settings.DefaultPatternIndex == -1) {
    <div class="message message-Error">@T("The current Content Type does not have a default Autoroute Pattern. Please edit the settings first.")</div>
    return;
}

@{
    var defaultPattern = Model.Settings.Patterns[Model.Settings.DefaultPatternIndex];
    var urlPrefix = WorkContext.Resolve<ShellSettings>().RequestUrlPrefix;
    if (!String.IsNullOrWhiteSpace(urlPrefix)) {
        urlPrefix += "/";
    }
}

@if (!Model.PromoteToHomePage) {
    <fieldset class="permalink">
        <label>@T("Permalink")</label>
        @if (Model.Settings.AllowCustomPattern) {
            <span>@ViewContext.RequestContext.HttpContext.Request.ToApplicationRootUrlString()/@urlPrefix</span><span class="autoroute-channel"></span>
            <span>@Html.TextBoxFor(m => m.CurrentUrl, new {@class = "text"})</span>
        }
        else {
            <span>@ViewContext.RequestContext.HttpContext.Request.ToApplicationRootUrlString()/@urlPrefix</span><span class="autoroute-channel"></span>
        }

        <span class="hint">@T("Save the current item and leave the input empty to have it automatically generated using the pattern {0} e.g., {1}", defaultPattern.Name, defaultPattern.Description)</span>

    </fieldset>
    if (AuthorizedFor(Permissions.SetHomePage)) {
         <fieldset>
             <span class="checkbox-and-label">
                 @Html.EditorFor(m => m.PromoteToHomePage)
                 <label for="@Html.FieldIdFor(m => m.PromoteToHomePage)" class="forcheckbox">@T("Set as home page")</label>
             </span>
             <span class="hint">@T("Check to promote this content as the home page")</span>
         </fieldset>
     }
}
else {
    <span>@T("This content is the current home page")</span>
}

@{
    Script.Require("jQuery").AtHead();
    var routeData = HttpContext.Current.Request.RequestContext.RouteData.Values;
    var isEdit = false;
    var isPublished = false;
    if (routeData["action"].ToString() == "Edit")
    {
        var id = Convert.ToInt16(routeData["id"]);
        isEdit = true;
        isPublished = WorkContext.Resolve<IContentManager>().Get(id, VersionOptions.Latest).HasPublished();
    }
}


@if (isPublished || Model.PromoteToHomePage)
{
    <script>
        $(function () {
            $("fieldset[data-id-prefix='AutoroutePart_Channels']").hide();
        });
    </script>
}
else
{
    <script>
            $(function () {
                var fieldset = $("fieldset[data-id-prefix='AutoroutePart_Channels']");
                $(fieldset).find("ul.terms").prepend("<li><input type='radio' data-slug='' value='-1' name='" + $(fieldset).attr("data-name-prefix") + ".SingleTermId'><label class='forcheckbox'>None</label></li>");
                $(fieldset).find("input[type=radio]").on('click', function () { $("span.autoroute-channel").text($(this).attr("data-slug")); });
                if ($(fieldset).find("input[type=radio]:checked").length == 0) {
                    $(fieldset).find("input[type=radio][value=-1]").attr("checked", "checked");
                }
                else {
                    $(fieldset).find("input[type=radio]:checked").click();
                }
            });
    </script>
}

@{
    if (routeData["area"].ToString() == "Orchard.Taxonomies" ||
        (routeData["action"].ToString() == "Create" && routeData["id"].ToString() == "Taxonomy"))
    {
        <script>
            $(function () {
                $("fieldset[data-id-prefix='AutoroutePart_Channels']").hide();
            });
        </script>
    }
}